package com.kelvin.paopao.module.main.date.datelist;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.paopao.R;
import com.kelvin.paopao.api.responseBean.DateListModel;
import com.kelvin.paopao.injector.components.DaggerMainDateComponent;
import com.kelvin.paopao.injector.modules.MainDateModule;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.main.date.datedetail.DateDetailActivity;
import com.kelvin.paopao.module.person.detail.PersonDetailActivity;
import com.kelvin.paopao.module.person.photoset.PhotoSetActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.utils.LoginUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


/**
 * Created by Administrator on 2017/5/9.
 */
public class MainDateListFragment extends BaseFragment<IMainDateListPresenter>
        implements IMainDateView {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private int type;

    @Inject
    BaseQuickAdapter adapter;
    private LinearLayoutManager manager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_date_list;
    }

    @Override
    protected void initInjector() {
        DaggerMainDateComponent.builder().mainDateModule(new MainDateModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        type = getArguments().getInt("type");
        manager = new LinearLayoutManager(getContext());

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (Constant.IS_LOGIN) {
                    DateDetailActivity.launch(getContext(),
                            (DateListModel) adapter.getData().get(position));
                } else {
                    LoginUtil.checkLogin(mContext);
                }
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<String> urls = new ArrayList<String>();
                urls.add(dateListModelList.get(position).getImg1());
                urls.add(dateListModelList.get(position).getImg2());
                urls.add(dateListModelList.get(position).getImg3());
                if (view.getId() == R.id.iv_content1) {
                    PhotoSetActivity.launch(getContext(), urls, 0);
                }
                if (view.getId() == R.id.iv_content2) {
                    PhotoSetActivity.launch(getContext(), urls, 1);
                }
                if (view.getId() == R.id.iv_content3) {
                    PhotoSetActivity.launch(getContext(), urls, 2);
                }
                if (view.getId() == R.id.iv_avatar) {
                    PersonDetailActivity.launch(getContext(), dateListModelList.get(position).getUserno());
                }
            }
        });
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh, type);
    }


    public static MainDateListFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        MainDateListFragment fragment = new MainDateListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private List<DateListModel> dateListModelList;

    @Override
    public void loadData(List<DateListModel> dateListModelList) {
        this.dateListModelList = dateListModelList;
        Collections.reverse(dateListModelList);
        adapter.setNewData(dateListModelList);
    }
}
